package com.mz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.common.dto.GlobalException;
import com.mz.common.dto.PaginationVo;
import com.mz.dao.UmsRoleInfoDao;
import com.mz.dao.UmsRolePermissionDao;
import com.mz.entity.UmsPermissionInfo;
import com.mz.entity.UmsRoleInfo;
import com.mz.entity.UmsRolePermission;
import com.mz.entity.vo.RoleVo;
import com.mz.entity.vo.UmsRoleInfoQuery;
import com.mz.service.UmsRoleInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * (UmsRoleInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-12-13 13:38:36
 */
@Service
public class UmsRoleInfoServiceImpl  implements UmsRoleInfoService {

    @Resource
    private UmsRoleInfoDao umsRoleInfoDao;
    @Resource
    private UmsRolePermissionDao umsRolePermissionDao;

    @Override
    public IPage getRoleList(PaginationVo paginationVo, UmsRoleInfoQuery umsRoleInfoQuery) throws Exception {
        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        umsRoleInfoDao.selectPage(page, null);

        /**
         * 获取角色对应的权限
         * */
        List<UmsRoleInfo> roleList = page.getRecords();
        Set<Long> roleIdSet = roleList.stream().map(item -> {
            return item.getId();
        }).collect(Collectors.toSet());

        LambdaQueryWrapper<UmsRolePermission> lqs = new LambdaQueryWrapper<>();
        lqs.in( UmsRolePermission::getRoleId, roleIdSet );
        List<UmsRolePermission> umsRolePermissions = umsRolePermissionDao.selectList(lqs);
        if( umsRolePermissions == null || umsRolePermissions.size() == 0 ){
            return page;
        }
        for( UmsRoleInfo roleInfo : roleList ){
            List<UmsRolePermission> filterPerms = umsRolePermissions.stream().filter(item -> {
                return item.getRoleId().equals(roleInfo.getId());
            }).collect(Collectors.toList());
            roleInfo.setPermissionList(filterPerms.stream().map(item->item.getPermissionId()).collect(Collectors.toList()));
        }
        return page;
    }


    @Override
    @Transactional
    public void addRole(RoleVo roleVo) throws Exception {

        if( roleVo.getId() != null ){
            throw GlobalException.gen(10012, "id值存在，接口调用错误");
        }
        //判断是否同名存在
        LambdaQueryWrapper<UmsRoleInfo> lqs = new LambdaQueryWrapper();
        lqs.eq( UmsRoleInfo::getName, roleVo.getRoleName() );
        UmsRoleInfo umsRoleInfoInSQL = umsRoleInfoDao.selectOne(lqs);
        if( umsRoleInfoInSQL != null ){
            throw GlobalException.gen(10012, "角色已经存在");
        }

        //创建role。
        UmsRoleInfo umsRoleInfo = new UmsRoleInfo();
        umsRoleInfo.setName(roleVo.getRoleName());
        umsRoleInfoDao.insert(umsRoleInfo);

        for( Long permissionId: roleVo.getPermissionList() ){
            UmsRolePermission umsRolePermission = new UmsRolePermission();
            umsRolePermission.setRoleId(umsRoleInfo.getId());
            umsRolePermission.setPermissionId(permissionId);
            umsRolePermissionDao.insert(umsRolePermission);
        }
    }

    @Override
    @Transactional
    public void updateRole(RoleVo roleVo) throws Exception {
        if( roleVo.getId() == null ){
            throw GlobalException.gen(10012, "id属性不存在");
        }

        //删除 roleId 对应的 permissionId;
        LambdaQueryWrapper<UmsRolePermission> lqs = new LambdaQueryWrapper<>();
        lqs.eq( UmsRolePermission::getRoleId, roleVo.getId() );
        umsRolePermissionDao.delete( lqs );

        for( Long permissionId: roleVo.getPermissionList() ){
            UmsRolePermission umsRolePermission = new UmsRolePermission();
            umsRolePermission.setRoleId(roleVo.getId());
            umsRolePermission.setPermissionId(permissionId);
            umsRolePermissionDao.insert(umsRolePermission);
        }
    }

    @Override
    public List<UmsRoleInfo> getRoleAllListNotPermission() throws Exception {

        List<UmsRoleInfo> umsRoleInfos = umsRoleInfoDao.selectList(null);
        return umsRoleInfos;
    }

    @Override
    public void removeRole(List<Long> idList) throws Exception {

        if( idList == null || idList.size() == 0 ){
            throw GlobalException.gen(10012, "角色id不存在");
        }

        for( Long roleId : idList ){
            if( new Long(1).equals(roleId) ){
                throw GlobalException.gen(10013, "超级管理员不允许删除");
            }
        }
        umsRoleInfoDao.deleteBatchIds(idList);
    }

    @Override
    public List<UmsRolePermission> getRolePermissionList(Long roleId) throws Exception {

        LambdaQueryWrapper<UmsRolePermission> lqs = new LambdaQueryWrapper<>();
        lqs.eq(UmsRolePermission::getRoleId, roleId);
        List<UmsRolePermission> umsRolePermissions = umsRolePermissionDao.selectList(lqs);
        return umsRolePermissions;
    }


}

